package com.biscuit.page.basicservice.design.search.factory;

import com.biscuit.page.basicservice.design.search.SearchHandlerContext;
import com.biscuit.page.basicservice.design.search.strategy.*;
import com.biscuit.page.basicservice.model.enums.SearchEngineTypeEnum;
import com.biscuit.page.basicservice.utils.BaseExceptionUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author biscuit
 * @createTime 2024/6/6 11:30
 */
public class SearchHandlerFactory {

    private final static Map<SearchEngineTypeEnum, SearchHandlerStrategy> STRATEGY_MAP;

    static {
        STRATEGY_MAP = new HashMap<>();
        STRATEGY_MAP.put(SearchEngineTypeEnum.BAI_DU, new BaiduSearchHandlerStrategy());
        STRATEGY_MAP.put(SearchEngineTypeEnum.BING, new BingSearchHandlerStrategy());
        STRATEGY_MAP.put(SearchEngineTypeEnum.V_360, new V360SearchHandlerStrategy());
        STRATEGY_MAP.put(SearchEngineTypeEnum.SO_GOU, new SoGouSearchHandlerStrategy());
    }

    public static SearchHandlerStrategy createStrategy(SearchHandlerContext context) {
        SearchHandlerStrategy searchHandlerStrategy = STRATEGY_MAP.get(context.getSearchEngineTypeEnum());
        BaseExceptionUtil.throwException(Objects.nonNull(searchHandlerStrategy), "相关搜索策略不存在");
        searchHandlerStrategy.setContext(context);
        return searchHandlerStrategy;
    }

}
